﻿using System;
using System.IO;
using System.Text;
using Ionic.Zip;
using Ro.AssistFunction.ExtentReport;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;

namespace Ro.Interpreter.InterAssistFunc
{
    /// <summary>
    /// logfuncion相关节点的实际操作
    /// </summary>
    public class SaveLogIntoFloders
    {
        /// <summary>
        ///logfuncion相关节点的实际操作
        /// </summary>
        /// <param name="logFunction">logfunction信息</param>
        /// <returns>返回是否完成</returns>
        public static bool SaveLogIntoFolder(LogFunction logFunction)
        {
            //所谓的复制log,是将c：/Temp 中的临时文件复制成一份新的给桌面文件夹内,同时还要将桌面打包一份的复制一份到给定路径

            try
            {
                //log中结束任务
                IRoExtentReport roExtentReport = new RoExtentReport();
                roExtentReport.EndExtentReports();
                //如果路径没有，直接在桌面给出一个包
                string savepath;


                if (string.IsNullOrEmpty(logFunction.LogFilePath))
                {
                    savepath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                }
                else
                {
                    if (!Directory.Exists(logFunction.LogFilePath))
                    {
                        Directory.CreateDirectory(logFunction.LogFilePath);
                    }
                    savepath =logFunction.LogFilePath;
                }
                       
                ZipFile zipFile = new ZipFile(Encoding.UTF8);
                zipFile.AddDirectory(RoPubArgments.TempLogInfo.FolderPath);
                //获取文件名
                string info = Path.GetFileNameWithoutExtension(RoPubArgments.ProTips ? RoPubArgments.GuiTestArgsInfo.TcsFilePath : RoPubArgments.RoTestArgsInfo.TcsFilePath);

                //获取当前时间
                string nowtime = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
                //存放
                zipFile.Save(savepath + "/" + info + "_(" + nowtime + ")" + ".zip");
                return true;
            }
            catch (Exception e)
            {
                //添加输出
                ExceptionOutPut.SystemExceptionOut("SaveLogIntoFolder", e);
                return false;
            }
        }
    }
}
